package ggaviator

import (
	"github.com/shopspring/decimal"
	"math"
	"math/rand"
	"time"
	"wzgames/pkg/common/timingwheel"
	"wzgames/pkg/utils/calcutil"
	"wzgames/pkg/utils/dateutil"
)

// 播报同步校验运算

type AMultiplier struct {
	currentMultiplier float64
	fixMultiplier     float64
	currentGameTime   int64
	timestep          int64
	roundId           int64
	maxMultiplier     float64
	interval          time.Duration
	multiTimer        *timingwheel.Timer

	houseEdge   float64
	houseEdgeAj decimal.Decimal
	maxv        int64
	ma          float64

	core *AirCraft
}

func (am *AMultiplier) Init(c *AirCraft) error {
	am.core = c
	am.timestep = 100
	am.interval = time.Duration(am.timestep) * time.Millisecond
	rand.Seed(dateutil.UnixNano())
	am.maxv = 4503599627370496
	am.ma = 2.265

	return nil
}

func (am *AMultiplier) CurrentMultiplier() float64 {
	return am.fixMultiplier
}

func (am *AMultiplier) SetHouseEdge(houseEdge float64) {
	am.houseEdge = houseEdge
	am.houseEdgeAj = decimal.NewFromInt32(100 - 1).Div(decimal.NewFromInt32(100).Sub(decimal.NewFromFloat(am.houseEdge)))
}

func (am *AMultiplier) Reset() {
	am.currentGameTime = 0
	if am.multiTimer != nil {
		am.multiTimer.Stop()
	}
}

func (am *AMultiplier) Run(roundId int64, maxMultiplier float64) {
	am.roundId = roundId
	am.maxMultiplier = maxMultiplier
	am.currentMultiplier = 1.0
	am.fixMultiplier = 1.0
	//am.ExpMultiplier(nil)
	am.multiTimer = am.core.TimerAfter(am.interval, am.ExpMultiplier, nil)
}

func (am *AMultiplier) calcMultiplier(gametime int64) float64 {
	return math.Pow(am.ma, 0.0001*float64(gametime))
}

func (am *AMultiplier) ExpMultiplier(data interface{}) {
	am.currentGameTime += am.timestep
	am.currentMultiplier = am.calcMultiplier(am.currentGameTime)
	if am.currentMultiplier >= am.maxMultiplier {
		am.fixMultiplier = am.maxMultiplier
		am.currentMultiplier = am.maxMultiplier
		//am.core.doMultiplier(am.fixMultiplier, am.currentGameTime)
		// crash时同步更新状态
		am.core.crash(am.roundId, am.maxMultiplier)
	} else {
		vfix := calcutil.RoundFloor(am.currentMultiplier)
		am.fixMultiplier = vfix
		//am.core.doMultiplier(vfix, am.currentGameTime)
		am.multiTimer = am.core.TimerAfter(am.interval, am.ExpMultiplier, nil)
	}
}
